﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TSCC.WinControl
{
    /// <summary>
    /// InsertRunning..連續新增模式
    /// </summary>
    public enum CRUDMode
    {
        None
        , Insert, InsertExecute//, InsertRunning
        , Update, UpdateExecute
        , Delete
        , Cancel
        , Query, QueryExecute,QueryAfter
        , Exit
    }   


    public partial class ucCRUD : UserControl
    {
        protected CRUDMode currentMode = CRUDMode.None;
        public CRUDMode CurrentMode
        {
            set
            {
                setAllBtnDisable();
                switch ((int)value)
                { 
                    case (int)CRUDMode.None:
                    case (int)CRUDMode.Cancel:
                        noneMode();
                        break;
                    case (int)CRUDMode.Insert:
                    case (int)CRUDMode.Update:
                        insUpdMode();
                        ucBtnExecute.ButtonnMode = (value == CRUDMode.Insert) 
                            ?CRUDMode.InsertExecute:CRUDMode.UpdateExecute;
                        break;
                    case (int)CRUDMode.Delete:
                        break;
                    case (int)CRUDMode.Query:
                        queryMode();
                        ucBtnExecute.ButtonnMode = CRUDMode.QueryExecute;
                        break;
                    case (int)CRUDMode.InsertExecute:
                        insUpdMode();
                        break;
                    //case (int)CRUDMode.InsertRunning:
                    //    insertRunningMode();
                    //    break;
                    case(int)CRUDMode.QueryAfter:
                        queryAfterMode();
                        break;
                }
                currentMode = value;   
            }
            get
            {
                return currentMode;
            }
        }

        public event EventHandler CRUDClick = null;

        public ucCRUD()
        {
            InitializeComponent();
            ucBtnInsert.Click += new EventHandler(btn_Click);
            ucBtnUpdate.Click += new EventHandler(btn_Click);
            ucBtnDelete.Click += new EventHandler(btn_Click);
            ucBtnQuery.Click += new EventHandler(btn_Click);
            ucBtnCancel.Click += new EventHandler(btn_Click);
            ucBtnExecute.Click += new EventHandler(btn_Click);
            ucBtnExit.Click += new EventHandler(btn_Click);
        }

        public void SetMode(CRUDMode p_mode)
        {
            CurrentMode = p_mode;
        }

        protected void queryAfterMode()
        {
            queryMode();
            ucBtnUpdate.Enabled = true;
            ucBtnDelete.Enabled = true;
        }

        protected void queryMode()
        {           
            ucBtnCancel.Enabled = true;
            ucBtnExecute.Enabled = true;
        }

        protected void insUpdMode()
        {            
            ucBtnExecute.Enabled = true;
            ucBtnCancel.Enabled = true;
        }

        protected void noneMode()
        {
            ucBtnInsert.Enabled = true;
            ucBtnQuery.Enabled = true;
            ucBtnExit.Enabled = true;
        }

        //protected void insertRunningMode()
        //{
        //    //ucBtnExecute.Enabled = true;
        //    //ucBtnCancel.Enabled = true;
        //    insUpdMode();
        //}

        protected void btn_Click(object sender, EventArgs e)
        {
            ucBtn btn = sender as ucBtn;
            CurrentMode = btn.ButtonnMode;
            if (CRUDClick != null)
            {
                CRUDClick(sender, e);
            }


        }

        protected void setAllBtnDisable()
        {
            ucBtnQuery.ButtonnMode = CRUDMode.Query;
            foreach (Control cnt in this.Controls)
            {
                if (cnt.GetType().Name.Equals("ucBtn"))
                {
                    cnt.Enabled = false;
                }
            }
        }
    }
}
